#!/usr/bin/python
#
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Contains the objects required for RATS job resource access. """

try:
  from xml.etree import cElementTree as ElementTree
except ImportError:
  try:
    import cElementTree as ElementTree
  except ImportError:
    from elementtree import ElementTree

import atom
import rawsc
from rawsc import RAWS_NAMESPACE
from rawsc.rats import *


class ReportJob(atom.AtomBase):
    """ Handler for the atom:content element, in a RAWS context. """
    _tag = 'job'
    _namespace = RAWS_NAMESPACE
    _attributes = {}
    _attributes['action'] = 'action'
    _attributes['msg'] = 'msg_attr' # need to call this differently, since we already have a datamember called msg (child-elem)
    _attributes['profile'] = 'profile'
    _children = {}
    _children['{%s}src_location' % (RAWS_NAMESPACE)] = ('src_location', SrcLocation)
    _children['{%s}msg' % (RAWS_NAMESPACE)] = ('msg', Msg)
    _children['{%s}start_time' % (RAWS_NAMESPACE)] = ('start_time', StartTime)
    _children['{%s}end_time' % (RAWS_NAMESPACE)] = ('end_time', EndTime)
    _children['{%s}local_uri' % (RAWS_NAMESPACE)] = ('local_uri', LocalUri)
    _children['{%s}src_filesize' % (RAWS_NAMESPACE)] = ('src_filesize', SrcFilesize)
    _children['{%s}tgt_filesize' % (RAWS_NAMESPACE)] = ('tgt_filesize', TgtFilesize)
    _children['{%s}public_uri' % (RAWS_NAMESPACE)] = ('public_uri', PublicUri)
    _children['{%s}export_uri' % (RAWS_NAMESPACE)] = ('export_uri', ExportUri)
    _children['{%s}method' % (RAWS_NAMESPACE)] = ('method', Method)

    def __init__(self, extension_elements=None, extension_attributes=None, text=None):
        self.extension_elements = extension_elements or []
        self.extension_attributes = extension_attributes or {}
        self.text = text
        self.src_location = SrcLocation()
        self.msg = Msg()
        self.start_time = StartTime()
        self.end_time = EndTime()
        self.local_uri = LocalUri()
        self.src_filesize = SrcFilesize()
        self.tgt_filesize = TgtFilesize()
        self.public_uri = PublicUri()
        self.export_uri = ExportUri()
        self.method = Method()
        

def ReportJobFromString(xml_string):
    return atom.CreateClassFromXMLString(ReportJob, xml_string)   


class Report(atom.AtomBase):
    """ Handler for the atom:content element, in a RAWS context. """
    _tag = 'report'
    _namespace = RAWS_NAMESPACE
    _attributes = {}
    _attributes['id'] = 'id'
    _attributes['status'] = 'status'
    _children = {}
    _children['{%s}job' % (RAWS_NAMESPACE)] = ('job', [ReportJob])
    
    job = None

    def __init__(self, extension_elements=None, extension_attributes=None, text=None):
        self.extension_elements = extension_elements or []
        self.extension_attributes = extension_attributes or {}
        self.text = text

def ReportFromString(xml_string):
    return atom.CreateClassFromXMLString(Report, xml_string)   


class Reports(atom.AtomBase):
    """ Handler for the atom:content element, in a RAWS context. """
    _tag = 'reports'
    _namespace = RAWS_NAMESPACE
    _attributes = {}
    _children = {}
    _children['{%s}report' % (RAWS_NAMESPACE)] = ('report', Report)

    report = None

    def __init__(self, report=None, extension_elements=None, extension_attributes=None, text=None):
        self.report = report
        self.extension_elements = extension_elements or []
        self.extension_attributes = extension_attributes or {}
        self.text = text

def ReportsFromString(xml_string):
    return atom.CreateClassFromXMLString(Reports, xml_string)   


class JobParams(rawsc.RawsParams):
    """ Handler for the <raws:params> element (= sub-element of atom:content). """
    _children = {}
    _children['{%s}id' % rawsc.RAWS_NAMESPACE] = ('id', Id)
    _children['{%s}status' % RAWS_NAMESPACE] = ('status', Status)
    _children['{%s}input' % RAWS_NAMESPACE] = ('input', Input)
    _children['{%s}output' % RAWS_NAMESPACE] = ('output', Output)
    _children['{%s}format' % RAWS_NAMESPACE] = ('format', Format)
    _children['{%s}src_location' % RAWS_NAMESPACE] = ('src_location', SrcLocation)
    _children['{%s}import_location' % RAWS_NAMESPACE] = ('import_location', ImportLocation)
    _children['{%s}tgt_location' % RAWS_NAMESPACE] = ('tgt_location', TgtLocation)
    _children['{%s}startpos' % RAWS_NAMESPACE] = ('startpos', Startpos)
    _children['{%s}endpos' % RAWS_NAMESPACE] = ('endpos', Endpos)
    _children['{%s}client_passthru' % RAWS_NAMESPACE] = ('client_passthru', ClientPassthru)
    _children['{%s}client_input' % RAWS_NAMESPACE] = ('client_input', ClientInput)
    _children['{%s}transc' % RAWS_NAMESPACE] = ('transc', Transc)
    _children['{%s}transc_media' % RAWS_NAMESPACE] = ('transc_media', TranscMedia)

    def __init__(self, id=None, status=None, input=None, output=None, format=None, src_location=None, import_location=None, tgt_location=None, startpos=None, endpos=None, client_passthru=None, client_input=None, transc=None, transc_media=None):
        rawsc.RawsParams.__init__(self, extension_elements=None, extension_attributes=None)
        self.id = Id(text = id)
        self.status = Status(text = status)
        self.input = Input(text = input)
        self.output = Output(text = output)
        self.format = Format(text = format)
        self.src_location = SrcLocation(text = src_location)
        self.import_location = ImportLocation(text = import_location)
        self.tgt_location = TgtLocation(text = tgt_location)
        self.startpos = Startpos(text = startpos)
        self.endpos = Endpos(text = endpos)
        self.client_passthru = ClientPassthru(text = client_passthru)
        self.client_input = ClientInput(text = client_input)
        self.transc = Transc(text = transc)
        self.transc_media = TranscMedia(text = transc_media)

def JobParamsFromString(xml_string):
    return atom.CreateClassFromXMLString(JobParams, xml_string)   


class JobContent(rawsc.RawsContent):
    """ Handler for the atom:content element, in a RAWS context. """
    _children = atom.AtomBase._children.copy()
    _children['{%s}params' % (RAWS_NAMESPACE)] = ('params', JobParams)
    
    params = None
    
    def __init__(self, params=None):
        rawsc.RawsContent.__init__(self, extension_elements=None, extension_attributes=None)
        self.params = Params()
        if params:
            self.params = params
    
def JobContentFromString(xml_string):
    return atom.CreateClassFromXMLString(JobContent, xml_string)   
    

class JobEntry(rawsc.RawsEntry):
    """ Handler for an entry that is returned by the RATS job resource."""
    _children = rawsc.RawsEntry._children.copy()
    _children['{%s}content' % atom.ATOM_NAMESPACE] = ('content', JobContent)
    _children['{%s}reports' % RAWS_NAMESPACE] = ('reports', Reports)

    reports = None

    def __init__(self, author=None, category=None, atom_id=None, link=None, published=None, title=None, updated=None, content = None, extension_elements=None, extension_attributes=None):
        rawsc.RawsEntry.__init__(self, author=author, category=category, atom_id=atom_id, link=link, published=published, title=title, updated=updated,
                                extension_elements=extension_elements, extension_attributes=extension_attributes)
        self.reports = None
        self.content = JobContent()
        if content:
            self.content = content

def JobEntryFromString(xml_string):
    return atom.CreateClassFromXMLString(JobEntry, xml_string)
    

class JobFeed(rawsc.RawsFeed, rawsc.LinkFinder):
    """ Handler for a feed that is returned by the RATS job resource."""
    _children = rawsc.RawsFeed._children.copy()
    _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', [JobEntry])

def JobFeedFromString(xml_string):
  return atom.CreateClassFromXMLString(JobFeed, xml_string)

